const http = require('http')
const config=require('../config')
const math=require('mathjs')
const qs=require('querystring')
const BigNumber = require('bignumber.js');

const getSum = function (d) {
    let sum = 0
    for(let i = 0; i < 3; i++){
        let t =  d % 10
        sum += t
        d = Math.floor(d/10)
    }
    return sum
}

const pad = function (num, n) {
    let len = num.toString().length;
    while(len < n) {
        num = "0" + num;
        len++;
    }
    return num;
}

const getResult = async (ctx,next) => { //查询用户资金历史
    let {lotteryType,date}=ctx.request.body
    let lotteryTypeInt = parseInt(lotteryType)
    if(isNaN(lotteryTypeInt) ||lotteryTypeInt <1 || lotteryTypeInt > 4 ){
        ctx.success({})

    }
    else{
        let url = '/lotteryResult/BTS/' + date

        const result = new Promise((resolve,reject) => {
            let opt={
                hostname: 'lottery.play.top',
                port: 80,
                path: url,
                method: "GET"
            }
            let req=http.request(opt,(res) => {
                res.setEncoding('utf8')
                let resdata=''
                res.on('data',(chunk) => {
                    resdata+=chunk
                })
                res.on('end',() => {
                    if(resdata == ''){
                        resolve({code:-80001,msg:'input invaild'})
                    }
                    else{
                        let data=JSON.parse(resdata)
                        resolve(data)
                    }

                })
            })
            req.on('error',(e) => {
                reject(e)
            })
            req.end()
        })
        let data=await result
        if(data.code){
            ctx.success({})
        }else{
            if(lotteryTypeInt == 1){ //时时彩
                let vailString = data.hash.slice(8)
                let hashData =  new BigNumber( vailString, 16)
                //console.log(data.hash)
                //console.log(hashData)
                let lastFive = hashData.mod(100000,10)
                let res = lastFive.toNumber()
                let tmp = {height:data.height,hash:hashData.toString(10),result:res}
                ctx.success({data:tmp})
            }
            else if(lotteryTypeInt == 2){ //pk10
                let vailString = data.hash.slice(8)
                let hashData =  new BigNumber( vailString, 16)
                let group = []
                for(let i = 0; i < 10; i++){
                    let q = hashData.mod(1000,10)
                    let o = q.toNumber()
                    group.push({d:getSum(o),t:pad(i + 1,2)})
                    hashData = hashData.dividedToIntegerBy(1000,10)
                }
                group.sort(function(a, b){
                    return b.d - a.d;
                })

                let res = ''
                for(let i = 0; i<group.length; i++){
                    res += '[' + group[i].t + '] '
                }
                let tmp = {height:data.height,hash:hashData.toString(10),result:res}
                ctx.success({data:tmp})

            }else if(lotteryTypeInt == 3){
                let vailString = data.hash.slice(8)
                let hashData =  new BigNumber( vailString, 16)
                let group = []
                for(let i = 0; i < 11; i++){
                    let q = hashData.mod(1000,10)
                    let o = q.toNumber()
                    group.push({d:getSum(o),t:pad(i + 1,2)})
                    hashData = hashData.dividedToIntegerBy(1000,10)
                }
                group.sort(function(a, b){
                    return b.d - a.d;
                })

                let res = ''
                for(let i = 0; i<5; i++){
                    res += '[' + group[i].t + '] '
                }
                let tmp = {height:data.height,hash:hashData.toString(10),result:res}
                ctx.success({data:tmp})
            }else if(lotteryTypeInt == 4){
                let vailString = data.hash.slice(8)
                let hashData =  new BigNumber( vailString, 16)
                //console.log(data.hash)
                //console.log(hashData)
                let lastThree = hashData.mod(1000,10)
                let res = lastThree.toNumber()
                let tmp = {height:data.height,hash:hashData.toString(10),result:res}
                ctx.success({data:tmp})
            }

        }
    }

    /*
    let data1 = {
        height:'26882627',
        hash: '019a32438f55eae683eb6b153a605c632a5ada52',
        result:'xxxxxxxxxxxx'
    }
    ctx.success({data1})
    */
}

module.exports={
    getResult
}